package UIFree.components
{
	import UIFInterface.ILabel;
	import UIFInterface.IWindow;
	
	import UIFree.controls.uif_close;
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class uif_window extends uif_ecomponent implements IWindow {

		protected var _title		: uif_label;
		protected var _close		: uif_component;
		protected var _canClose		: int;
		protected var _modalGurtain	: uif_ecomponent;
		
		public static const CLOSEWINDOW: String ='CLOSEWINDOW';
		
		public static const CLOSEDISPOSE	: int = 2;
		public static const CLOSEHIDE		: int = 1;
		
		public function uif_window(parent:uif_component, ax:int=0, ay:int=0, 
									awidth:int=0, aheight:int=0, styleClass:String='', 
									modal:Boolean=false, canClose: int = CLOSEDISPOSE) {
			super(parent, ax, ay, awidth, aheight, styleClass);
			visible = false;
			alpha = 0;
			_canClose = canClose;
			if (modal) asModal();
			_title = createTitle();
			_title.addEventListener(MouseEvent.MOUSE_DOWN, function(e: MouseEvent): void {
				beginDrag(5);
			});
			createDialogComponents();
			addEventListener(Event.CLOSE, doClose);
		}
		
		protected function createTitle(): uif_label {
			var inner: Rectangle = titleRect;
			var a_title: uif_label = new uif_label(this, inner.x, inner.y, inner.width, inner.height, style['title']);
			a_title.textField.height = 25;
			return a_title;
		}
		
		protected function get titleRect(): Rectangle {
			var inner: Rectangle = getInner();
			inner.x 		= inner.width / 40;
			inner.y 		= -18;
			inner.width 	= inner.width - inner.width / 3;
			inner.height 	= 25;
			return inner;
		}
		
		override public function resize(newWidth: Number, newHeight: Number): uif_component {
			super.resize(newWidth, newHeight);
			if (_title) {
				var tRect: Rectangle = titleRect;
				_title.resize(tRect.width, tRect.height);
			}
			if (_close) {
				_close.x = width - 30;
			}
			return this;
		}
		
		
		protected function createDialogComponents(): void {
			_close = createCloseButton();
		}
		
		protected function createCloseButton(): uif_component {
			return new uif_close(this);
		}
		
		protected function createModalGuard(): uif_ecomponent {
			var inner: Rectangle = app.getInner();
			return new uif_ecomponent(parent as uif_component, inner.x, inner.y, inner.width, inner.height, 
				style['gurtain']?style['gurtain']:'modalGurtain');
		}
		
		protected function asModal(): void {
			_modalGurtain = createModalGuard();
			_modalGurtain.visible = false;
			_modalGurtain.alpha = 0;
			parent.swapChildren(_modalGurtain, this);
		}
		
		public function show(onShow: Function = null): void {
			slideShow(onShow);
			if (_modalGurtain) _modalGurtain.slideShow();
		}
		
		protected function doClose(e: Event): void {
			if (parent != null) close(canClose());
		}
		
		public function close(canCloseType: int=0, onClose: Function=null): void {
			canCloseType = canCloseType?canCloseType:_canClose;
			switch (canCloseType) {
				case CLOSEHIDE: slideHide(function(): void {
							dispatchEvent(new Event(CLOSEWINDOW));
							if (onClose != null) onClose();
						});
						if (_modalGurtain != null) _modalGurtain.slideHide();
						break;
				case CLOSEDISPOSE: slideHide(function(): void {
							dispatchEvent(new Event(CLOSEWINDOW));
							if (onClose != null) onClose();
							dispose();
							if (_modalGurtain != null) _modalGurtain.dispose();
						});
						if (_modalGurtain != null) _modalGurtain.slideHide();
						break;
			}
		}
		
		protected function canClose(): int {
			return _canClose;
		}
		
		public function get title(): ILabel {
			return _title;
		}
	}
}